/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.freedom.imarket.controller.stream;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author edoardo
 */

@Controller
public class JSController {
    
    
    @RequestMapping(value={"/extjs/**/*.js","/*.js","/**/controller/*.js"})
    
    public  String getJS(HttpServletRequest request,HttpServletResponse response) throws IOException{
        response.setContentType("text/javascript");

        String uri = request.getRequestURI();
//        System.out.println("URI in ingresso: "+uri);
//        System.out.println("Context path:"+request.getContextPath() );
//        System.out.println("");
        uri = uri.substring(uri.indexOf(request.getContextPath())+request.getContextPath().length());
        System.out.println("--------> "+uri);
        System.out.println();
        File f = new File("");
//        System.out.println(f.getAbsolutePath());
//        System.out.println("URI: "+request.getRequestURI());
        String path = f.getAbsolutePath()+File.separator+request.getRequestURI();
//        System.out.println("path: "+path);
        InputStream in = request.getServletContext().getResource(uri).openStream();
        byte[] b = IOUtils.toByteArray(in);
//        System.out.println(new String(b));
        
        response.getOutputStream().write(b);
        
        return null;
    }
}
